package com.sxhuayuan.parking.controller.agent;

import com.sxhuayuan.parking.compenent.Page;
import com.sxhuayuan.parking.compenent.web.RespMessage;
import com.sxhuayuan.parking.controller.BaseController;
import com.sxhuayuan.parking.dto.OrderListDTO;
import com.sxhuayuan.parking.entity.Agent;
import com.sxhuayuan.parking.entity.Order;
import com.sxhuayuan.parking.service.AgentService;
import com.sxhuayuan.parking.service.OrderService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController("agentOrderControlelr")
@RequestMapping("/agent/order")
public class OrderController extends BaseController {

	@Resource(name = "orderServiceImpl")
	OrderService service;
	@Resource(name = "agentServiceImpl")
	AgentService agentService;

	/** 分页条件查询订单 */
	@PostMapping("/list")
	public RespMessage list(@RequestBody OrderListDTO dto) {
		String childrenAgent = dto.getChildrenAgent();
		Agent parent = agentService.getCurrent();
		if (childrenAgent != null && !"".equals(childrenAgent)) {
			Agent agent = agentService.findOne(Long.valueOf(childrenAgent));
			boolean permission = agentService.isPermission(parent, agent);
			if (!permission) {
				return RespMessage.error("无权查看此代理");
			}
			Page<Order> page = service.findPage(dto, agent);
			return successResp(page);
		}

		Page<Order> page = service.findPage(dto, parent);
		return successResp(page);
	}

	/**
	 * 分页查询订单的金额统计
	 *
	 * @return 订单金额
	 */
	@PostMapping("/countmoney")
	public RespMessage getCountMoney(@RequestBody OrderListDTO dto) {
		String childrenAgent = dto.getChildrenAgent();
		Agent parent = agentService.getCurrent();
		if (childrenAgent != null && !"".equals(childrenAgent)) {
			Agent agent = agentService.findOne(Long.valueOf(childrenAgent));
			boolean permission = agentService.isPermission(parent, agent);
			if (!permission) {
				return RespMessage.error("无权查看此代理");
			}
			return successResp(service.findCount(dto, agent));
		}
		return successResp(service.findCount(dto, parent));
	}
}
